package yingke.com.yingke.ui.left;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import yingke.com.yingke.R;
import yingke.com.yingke.factory.LeftFragmentFactory;
import yingke.com.yingke.ui.right.SearchActivity;

/**
 * 左侧按钮视图，里面主要包括关注，热门，以及附近三个模块，
 * 上部分左侧为搜索框，右侧为信息接受框
 */
public class LeftFragment extends Fragment implements View.OnClickListener {
    private RadioButton nearby;
    private RadioButton hot;
    private RadioButton attention;
    private RadioGroup rgTitle;
    private ViewPager vpTitle;
    private SparseArray<Fragment> list;
    private ImageView search;
    private ImageView more;

    public LeftFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_left, container, false);
    }
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        initData();

        setTitleVpagerAdapter();
        //设置监听器
        setListener();
    }

    private void initView(View view) {
        attention = (RadioButton) view.findViewById(R.id.rb_attention);
        hot = (RadioButton) view.findViewById(R.id.rb_hot);
        nearby = (RadioButton) view.findViewById(R.id.rb_nearby);
        rgTitle = (RadioGroup) view.findViewById(R.id.rg_left_title);
        view.findViewById(R.id.iv_left_search).setOnClickListener(this);
        more = (ImageView) view.findViewById(R.id.iv_left_more);
        vpTitle = (ViewPager) view.findViewById(R.id.vp_left_title);
        vpTitle.setOnClickListener(this);
        hot.setSelected(true);
        attention.setOnClickListener(this);
        search.setOnClickListener(this);


    }

    private void initData() {
        list = new SparseArray<>();
        for (int i = 0; i <3 ; i++) {
            Fragment fragment = LeftFragmentFactory.createFragment(i);
            list.put(i,fragment);
        }

    }

    private void setTitleVpagerAdapter() {
        LeftTitleAdapter adapter = new LeftTitleAdapter(getActivity().getSupportFragmentManager(), list);
        vpTitle.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rg_left_title:
                break;
            case R.id.iv_left_search:
                getActivity().startActivity(new Intent(getContext(), SearchActivity.class));
                break;
            case R.id.iv_left_more:

                break;
        }

    }


    private void setListener() {
        //给Viewpager设置监听，当viewPager切换到哪一页，就让radioButton选中对应的那一页
        vpTitle.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                for (int i = 0; i <3; i++) {
                    if(i==position){
                        ((RadioButton)rgTitle.getChildAt(position)).setChecked(true);
                        ((RadioButton)rgTitle.getChildAt(position)).setTextSize(20);
                    }else {
                        ((RadioButton)rgTitle.getChildAt(i)).setChecked(false);
                        ((RadioButton)rgTitle.getChildAt(i)).setTextSize(15);
                    }
                }
            }
        });
        //给RadioGroup设置监听，当RadioButton选中哪一页时，就让viewpager滑动到对应页
        rgTitle.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
               switch (checkedId){
                   case R.id.rb_attention:
                       attention.setChecked(true);
                       vpTitle.setCurrentItem(0);
                       attention.setTextSize(20);
                       hot.setTextSize(15);
                       nearby.setTextSize(15);
                       break;
                   case R.id.rb_hot:
                       hot.setChecked(true);
                       vpTitle.setCurrentItem(1);
                       attention.setTextSize(15);
                       hot.setTextSize(20);
                       nearby.setTextSize(15);
                       break;
                   case R.id.rb_nearby:
                       nearby.setChecked(true);
                       vpTitle.setCurrentItem(2);
                       attention.setTextSize(15);
                       hot.setTextSize(15);
                       nearby.setTextSize(20);
                       break;

               }
            }
        });

    }

}
